﻿using HoverTree.HoverTreeFrame;
using HoverTree.IDAL;
using HoverTree.Model;
using System.Collections.Generic;

namespace HoverTree.BLL
{
    public class HvtPanelUser
    {
        static readonly IPanelUser dal = DALFactory.DataAccess.CreatePanelUser();
        public string Add(PanelUserInfo panelUser)
        {
            panelUser.Hvtpassword = HoverString.GetMd5(panelUser.Hvtpassword, 2);
            return dal.Add(panelUser);
        }

        public string Alter(PanelUserInfo panelUser)
        {
            return dal.Alter(panelUser);
        }

        public string AlterPassword(string hvtusername, string passwordBefore, string passwordNew)
        {
            PanelUserInfo m_info = new PanelUserInfo();

            m_info.Hvtusername = hvtusername;
            m_info.Hvtpassword = passwordBefore;

            if (Login(m_info)!=string.Empty)
                return "原密码错误";

            m_info.Hvtpassword = HoverString.GetMd5(passwordNew,2);

            return dal.AlterPassword(m_info);
        }

        public PanelUserInfo Get(int hvtID)
        {
            return dal.Get(hvtID);
        }

        public PanelUserInfo GetByUserName(string userName)
        {
            return dal.GetByUserName(userName);
        }

        public string Login(PanelUserInfo info)
        {
            PanelUserInfo m_getInfo = dal.GetByUserName(info.Hvtusername);

            if (m_getInfo == null)
                return "用户名或者密码错误！";

            if (HoverString.GetMd5(info.Hvtpassword, 2) != m_getInfo.Hvtpassword)
                return "用户名或者密码错误";

            return string.Empty;

        }

        public List<PanelUserInfo> HvtPageQuery(int sortType, int pageSize, int pageIndex, out int totalCount, out int totalPageCount)
        {
            return dal.HvtPageQuery(sortType, pageSize, pageIndex, out totalCount, out totalPageCount);
        }

        public string Delete(int hvtid)
        {
            return dal.Delete(hvtid);
        }
    }
}